package com.intellagric.controller.portal.iftmonitor;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.service.search.IftDataRecordSearchService;
import com.intellagric.service.search.pojo.DataRecordVo;
import com.intellagric.service.search.pojo.IftDataRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2019/1/7
 * @DESC： 采集数据管理
 */
@Controller
public class DatarecordController {


    @Autowired
    private IftDataRecordSearchService dataRecordSearchService;

    /**
     * 查询数据记录信息列表，导入索引库
     * @return ResponseMessage
     */
    @RequestMapping("/search/datarecord/import")
    @ResponseBody
    public ResponseMessage importIndex() {
        return dataRecordSearchService.importDataRecord();
    }

    /**
     * 分页查询数据记录信息列表
     * @return LayuiDataGridResult
     */
    @RequestMapping("/search/datarecord/page")
    @ResponseBody
    public LayuiDataGridResult getPage(int page, int limit) {
        return dataRecordSearchService.searchDatarecordPage(page,limit);
    }

    /**
     * 多条件分页查询数据记录信息列表
     * @return LayuiDataGridResult
     */
    @RequestMapping("/search/datarecord/query")
    @ResponseBody
    public LayuiDataGridResult getWithRelationByVo(DataRecordVo dataRecordVo) {
        return dataRecordSearchService.searchDatarecordByVo(dataRecordVo);
    }

    /**
     * 多条件分页查询数据记录信息列表
     * @return LayuiDataGridResult
     */
    @RequestMapping("/search/datarecord/page/query")
    @ResponseBody
    public LayuiDataGridResult getPageWithRelationByVo(int page, int limit, DataRecordVo dataRecordVo) {
        return dataRecordSearchService.searchDatarecordPageByVo(dataRecordVo,page,limit);
    }

    /**
     * 多条件分页查询数据记录信息列表--数据汇总
     * @return LayuiDataGridResult
     */
    @RequestMapping("/search/datarecord/page/query/summary")
    @ResponseBody
    public LayuiDataGridResult getPageWithRelationByVoSummary(int page, int limit, int channelNum, DataRecordVo dataRecordVo) {
        LayuiDataGridResult result = dataRecordSearchService.searchDatarecordPageByVo(dataRecordVo, page, limit * channelNum);
        List<IftDataRecord> dataRecordList = result.getData();
        long total = (long) Math.ceil(result.getCount() / channelNum);

        //处理查询到的数据，进行数据汇总
        List<Map<String,Object>> newDatarecordList = new ArrayList<>();
        if (total < limit) {
            limit = (int) total;
        }
        for (int i = 0; i < limit;i++) {
            Map<String,Object> oneDataRecord = new HashMap<>();
            int count = 0;
            for (int j = channelNum * i;j < dataRecordList.size();j++) {
                if (oneDataRecord.size() - 3 >= channelNum) {   //如果通道数据综合完成
                    break;
                }
                oneDataRecord.put("equipmentName",dataRecordList.get(j).getEquipmentName());
                oneDataRecord.put("acquisitionTime",dataRecordList.get(j).getAcquisitionTime());
                oneDataRecord.put("reportingTime",dataRecordList.get(j).getReportingTime());
                oneDataRecord.put("c_" + dataRecordList.get(j).getCheckItemId(),dataRecordList.get(j).getProcessedData());
                count++;
            }
            newDatarecordList.add(oneDataRecord);
        }
        return LayuiDataGridResult.success().add(newDatarecordList,total);
    }

}
